package com.guangyunl.b_array;

import java.util.Arrays;

// 需求: 将arr1[]的元素复制到arr2[]上
// 方式二: 使用 System.arraycopy()
public class Demo07_02ArrayCopy {
    public static void main(String[] args) {
        // 定义数组arr1[]
        int[] arr1 = {1,2,3,4,5};
        // 定义数组arr2[]
        int[] arr2 = new int[arr1.length];
        // 复制 arr1 到 arr2
        System.arraycopy(arr1, 0, arr2, 0, arr1.length);
        //输出arr2[]
        for (int i = 0; i < arr2.length; i++) {
            System.out.println("arr2[" + i + "] = " + arr2[i]);
        }
        // 打印结果数组(以字符串的格式打印出来)
        System.out.println("arr2 = " + Arrays.toString(arr2));
    }
}

/*
arr2[0] = 1
arr2[1] = 2
arr2[2] = 3
arr2[3] = 4
arr2[4] = 5
arr2 = [1, 2, 3, 4, 5]
 */